home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows 95 API Bible
/
Windows 95 API Bible 3 Disc Set.iso
/
Win32 API Bible Book 3 of 3
/
CHAPTE21
/
AVIEDIT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-04-29
|
11KB
|
403 lines
#include <windows.h>
#include <stdio.h>
#include "AviEdit.h"
#include <vfw.h>
#if defined (WIN32)
#define IS_WIN32 TRUE
#else
#define IS_WIN32 FALSE
#endif
#define IS_NT IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
#define IS_WIN32S IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
#define IS_WIN95 (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
HINSTANCE hInst; // current instance
LPCTSTR lpszAppName = "MyApp";
LPCTSTR lpszTitle = "My Application";
// the rest of the stuff
//......................
BOOL RegisterWin95( CONST WNDCLASS* lpwc );
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
HWND hWnd;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (hInstance, lpszAppName);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = lpszAppName;
wc.lpszClassName = lpszAppName;
if ( IS_WIN95 )
{
if ( !RegisterWin95( &wc ) )
return( FALSE );
}
else if ( !RegisterClass( &wc ) )
return( FALSE );
hInst = hInstance;
hWnd = CreateWindow( lpszAppName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0,
NULL,
NULL,
hInstance,
NULL
);
if ( !hWnd )
return( FALSE );
ShowWindow( hWnd, nCmdShow );
UpdateWindow( hWnd );
while( GetMessage( &msg, NULL, 0, 0) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return( msg.wParam );
}
BOOL RegisterWin95( CONST WNDCLASS* lpwc )
{
WNDCLASSEX wcex;
wcex.style = lpwc->style;
wcex.lpfnWndProc = lpwc->lpfnWndProc;
wcex.cbClsExtra = lpwc->cbClsExtra;
wcex.cbWndExtra = lpwc->cbWndExtra;
wcex.hInstance = lpwc->hInstance;
wcex.hIcon = lpwc->hIcon;
wcex.hCursor = lpwc->hCursor;
wcex.hbrBackground = lpwc->hbrBackground;
wcex.lpszMenuName = lpwc->lpszMenuName;
wcex.lpszClassName = lpwc->lpszClassName;
// Added elements for Windows 95.
//...............................
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName,
IMAGE_ICON, 16, 16,
LR_DEFAULTCOLOR );
return RegisterClassEx( &wcex );
}
LRESULT CALLBACK About( HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return (TRUE);
case WM_COMMAND:
if ( LOWORD(wParam) == IDOK
|| LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, TRUE);
return (TRUE);
}
break;
}
return (FALSE);
}
#define MSG_LEN 1024
char msg[MSG_LEN+1];
VOID OpenAvi(HWND hWnd);
VOID EditAvi(HWND hWnd);
VOID CloseAvi();
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch( uMsg )
{
case WM_CREATE :
// initialize the AVI library DLL
//...............................
AVIFileInit();
break;
case WM_COMMAND :
switch( LOWORD(wParam) )
{
case IDM_TEST:
OpenAvi(hWnd);
EditAvi(hWnd);
CloseAvi();
break;
case IDM_ABOUT :
DialogBox( hInst, "AboutBox", hWnd, About );
break;
case IDM_EXIT :
DestroyWindow( hWnd );
break;
}
break;
case WM_DESTROY :
// release the AVI library DLL
//............................
AVIFileExit();
PostQuitMessage(0);
break;
default :
return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
}
return(0L);
}
/*****************************************************************************
* Global variables
*
*
*
******************************************************************************/
HRESULT rc;
LPCTSTR lpszInputFilename = "small.avi";
PAVIFILE pAviIn = NULL;
PAVIFILE pAviOut = NULL;
PAVISTREAM pAudioStream = NULL;
PAVISTREAM pVideoStream = NULL;
/*****************************************************************************
* OpenAvi
*
*
*
******************************************************************************/
VOID OpenAvi(HWND hWnd)
{
LONG i;
AVIFILEINFO pFileInfo;
PAVISTREAM pAviStream;
AVISTREAMINFO StreamInfo;
// open input AVI file
//....................
rc = AVIFileOpen(&pAviIn, lpszInputFilename, OF_READ, NULL);
if (rc) // if error
{
MessageBox(hWnd, "Error opening AVI file.",
NULL, MB_OK);
return;
}
// get input AVI file info
//........................
pFileInfo.dwStreams = 0L;
rc = AVIFileInfo(pAviIn, &pFileInfo, sizeof(AVIFILEINFO));
// locate AVI streams
//...................
for (i = 0; i < (LONG)pFileInfo.dwStreams; i++)
{
rc = AVIFileGetStream(pAviIn, &pAviStream, 0L, i);
if (rc)
break;
rc = AVIStreamInfo(pAviStream, &StreamInfo, sizeof(AVISTREAMINFO));
if (rc)
continue;
if (StreamInfo.fccType == streamtypeVIDEO)
{
pVideoStream = pAviStream;
AVIStreamAddRef(pVideoStream);
}
else if (StreamInfo.fccType == streamtypeAUDIO)
{
pAudioStream = pAviStream;
AVIStreamAddRef(pAudioStream);
}
}
}
/*****************************************************************************
* EditAvi
*
*
*
******************************************************************************/
VOID EditAvi(HWND hWnd)
{
LONG i;
PAVISTREAM pEditAudioStream;
PAVISTREAM pEditVideoStream;
PAVISTREAM pCloneAudioStream;
PAVISTREAM pReverseVideoStream;
PAVISTREAM pCutVideoStream;
AVISTREAMINFO StreamInfo;
PAVISTREAM pSaveStream[2];
AVICOMPRESSOPTIONS SaveOptions[2];
LPAVICOMPRESSOPTIONS pSaveOptions[2];
LONG nPos, nLen;
// create editable audio/video streams
//....................................
CreateEditableStream(&pEditAudioStream, pAudioStream);
CreateEditableStream(&pEditVideoStream, pVideoStream);
// clone the audio stream
//.......................
EditStreamClone(pEditAudioStream, &pCloneAudioStream);
if ( !AVIStreamInfo(pCloneAudioStream, &StreamInfo, sizeof(AVISTREAMINFO)) )
{
strcpy(StreamInfo.szName, "Cloned audio stream.");
EditStreamSetInfo(pCloneAudioStream, &StreamInfo, sizeof(AVISTREAMINFO));
}
// copy the video stream
//......................
nPos = AVIStreamStart(pEditVideoStream);
nLen = AVIStreamLength(pEditVideoStream);
EditStreamCopy(pEditVideoStream, &nPos, &nLen,
&pReverseVideoStream);
// cut the the back half of the copied stream
// and paste in at the beginning
// therefore flipping the first half of the video
// with the second half.
//...............................................
nPos = nLen / 2;
nLen -= nPos;
EditStreamCut(pReverseVideoStream, &nPos, &nLen,
&pCutVideoStream);
nPos = AVIStreamStart(pEditVideoStream);
EditStreamPaste(pReverseVideoStream, &nPos, &nLen,
pCutVideoStream, 0, -1);
EditStreamSetName(pReverseVideoStream, "1/2 reversed video stream.");
// create a new AVI file from the newly created streams
//.....................................................
pSaveStream[0] = pCloneAudioStream;
pSaveStream[1] = pReverseVideoStream;
// get save (compression) options
//...............................
for (i = 0; i < 2; i++)
{
// AVISaveOptions takes an array of pointers to our
// compression options (cleared)
pSaveOptions[i] = &SaveOptions[i];
memset(pSaveOptions[i], 0, sizeof(AVICOMPRESSOPTIONS));
}
AVISaveOptions(hWnd,
ICMF_CHOOSE_KEYFRAME | ICMF_CHOOSE_DATARATE |
ICMF_CHOOSE_PREVIEW,
2,
pSaveStream,
pSaveOptions);
remove("AviEdit.avi"); // destroy previous version (if any)
AVISave("AviEdit.avi", NULL, NULL, 2,
pSaveStream[0], pSaveOptions[0],
pSaveStream[1], pSaveOptions[1]);
// free save options resourses
//............................
AVISaveOptionsFree(2, pSaveOptions);
AVIStreamRelease(pEditAudioStream);
AVIStreamRelease(pEditVideoStream);
AVIStreamRelease(pCloneAudioStream);
AVIStreamRelease(pReverseVideoStream);
AVIStreamRelease(pCutVideoStream);
}
/*****************************************************************************
* CloseAvi
*
*
*
******************************************************************************/
VOID CloseAvi()
{
// release AVI streams
//....................
if (pVideoStream)
AVIStreamRelease(pVideoStream);
if (pAudioStream)
AVIStreamRelease(pAudioStream);
// close AVI files
//................
AVIFileRelease(pAviIn);
}